package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.CarOrderDetailBean;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.KuaituDialog;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.TimeUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarOrderDetailActivity extends BaseActivity {

    @BindView(R.id.detail_order_car_return)
    ImageView detailOrderCarReturn;
    @BindView(R.id.detail_order_car_phone)
    ImageView detailOrderCarPhone;
    @BindView(R.id.detail_order_toolbar_car)
    RelativeLayout detailOrderToolbarCar;
    @BindView(R.id.detail_order_car)
    TextView detailOrderCar;
    @BindView(R.id.detail_order_car_id)
    TextView detailOrderCarId;
    @BindView(R.id.detail_order_car_c)
    TextView detailOrderCarC;
    @BindView(R.id.detail_order_car_num)
    TextView detailOrderCarNum;
    @BindView(R.id.detail_order_car_brand)
    TextView detailOrderCarBrand;
    @BindView(R.id.detail_order_car_time0)
    TextView detailOrderCarTime0;
    @BindView(R.id.detail_order_car_tv_haoshi)
    TextView detailOrderCarTvHaoshi;
    @BindView(R.id.detail_order_car_iv_haoshi)
    ImageView detailOrderCarIvHaoshi;
    @BindView(R.id.detail_order_car_rl)
    AutoRelativeLayout detailOrderCarRl;
    @BindView(R.id.detail_order_car_orderTime)
    TextView detailOrderCarOrderTime;
    @BindView(R.id.detail_order_car_startTime)
    TextView detailOrderCarStartTime;
    @BindView(R.id.detail_order_car_startLocation)
    TextView detailOrderCarStartLocation;
    @BindView(R.id.detail_order_car_endTime)
    TextView detailOrderCarEndTime;
    @BindView(R.id.detail_order_car_endLocation)
    TextView detailOrderCarEndLocation;
    @BindView(R.id.detail_order_car_rl_haoshi)
    AutoLinearLayout detailOrderCarRlHaoshi;
    @BindView(R.id.detail_order_car_money)
    TextView detailOrderCarMoney;
    @BindView(R.id.detail_order_car_rjDj)
    TextView detailOrderCarRjDj;
    @BindView(R.id.detail_order_car_rjmoney)
    TextView detailOrderCarRjmoney;
    @BindView(R.id.detail_order_car_wjDj)
    TextView detailOrderCarWjDj;
    @BindView(R.id.detail_order_car_wjmoney)
    TextView detailOrderCarWjmoney;
    @BindView(R.id.detail_order_car_csDj)
    TextView detailOrderCarCsDj;
    @BindView(R.id.detail_order_car_csmoney)
    TextView detailOrderCarCsmoney;
    @BindView(R.id.detail_order_car_yhmoney)
    TextView detailOrderCarYhmoney;
    @BindView(R.id.detail_order_car_iv_yhmoney)
    ImageView detailOrderCarIvYhmoney;
    @BindView(R.id.detail_order_car_rlYh)
    AutoRelativeLayout detailOrderCarRlYh;
    @BindView(R.id.detail_order_car_rzyh)
    TextView detailOrderCarRzyh;
    @BindView(R.id.detail_order_car_rzyhmoney)
    TextView detailOrderCarRzyhmoney;
    @BindView(R.id.detail_order_car_swyh)
    TextView detailOrderCarSwyh;
    @BindView(R.id.detail_order_car_swmoney)
    TextView detailOrderCarSwmoney;
    @BindView(R.id.detail_order_car_zmth)
    TextView detailOrderCarZmth;
    @BindView(R.id.detail_order_car_zmthmoney)
    TextView detailOrderCarZmthmoney;
    @BindView(R.id.detail_order_car_zzth)
    TextView detailOrderCarZzth;
    @BindView(R.id.detail_order_car_zzthmoney)
    TextView detailOrderCarZzthmoney;
    @BindView(R.id.detail_order_car_ll_yh)
    AutoLinearLayout detailOrderCarLlYh;
    @BindView(R.id.detail_order_car_coupon)
    TextView detailOrderCarCoupon;
    @BindView(R.id.detail_order_car_tv_coupon)
    TextView detailOrderCarTvCoupon;
    @BindView(R.id.detail_order_car_coupon_rl)
    AutoRelativeLayout detailOrderCarCouponRl;
    @BindView(R.id.detail_order_car_zffs)
    TextView detailOrderCarZffs;
    @BindView(R.id.detail_order_car_tan)
    TextView detailOrderCarTan;
    @BindView(R.id.detail_order_car_sv)
    ScrollView detailOrderCarSv;
    @BindView(R.id.detail_progressBar_car_settlement)
    RelativeLayout detailProgressBarCarSettlement;
    @BindView(R.id.detail_order_car_tv_money)
    TextView detailOrderCarTvMoney;
    @BindView(R.id.detail_order_car_moneya)
    TextView detailOrderCarMoneya;
    @BindView(R.id.detail_order_car_bjmp)
    TextView detailOrderCarBjmp;
    @BindView(R.id.detailorder_car_bjmpmoney)
    TextView detailorderCarBjmpmoney;
    @BindView(R.id.detail_order_car_rsbz)
    TextView detailOrderCarRsbz;
    @BindView(R.id.detail_order_car_rsbzmoney)
    TextView detailOrderCarRsbzmoney;
    @BindView(R.id.detail_order_car_rsbz_rl)
    AutoRelativeLayout detailOrderCarRsbzRl;
    @BindView(R.id.detail_order_car_bjmp_rl)
    AutoRelativeLayout detailOrderCarBjmpRl;
    @BindView(R.id.detail_order_car_Yh_line)
    View detailOrderCarYhLine;
    @BindView(R.id.detail_order_car_Yh_block)
    View detailOrderCarYhBlock;
    @BindView(R.id.detail_order_car_rj_rl)
    AutoRelativeLayout detailOrderCarRjRl;
    @BindView(R.id.detail_order_car_rj_line)
    View detailOrderCarRjLine;
    @BindView(R.id.detail_order_car_wj_rl)
    AutoRelativeLayout detailOrderCarWjRl;
    @BindView(R.id.detail_order_car_wj_line)
    View detailOrderCarWjLine;
    @BindView(R.id.detail_order_car_cs_rl)
    AutoRelativeLayout detailOrderCarCsRl;
    @BindView(R.id.detail_order_car_cs_line)
    View detailOrderCarCsLine;
    private int flag1 = 0;
    private int flag = 0;
    private String orderId;
    private Context mContext = this;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_order_detail;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0,true);
        Intent intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        getCarOrderDetail();
    }

    private void getCarOrderDetail() {
        detailProgressBarCarSettlement.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getCarDetail(MyApplication.getInstance().getRequestFieldMap(),orderId, "1").enqueue(new Callback<CarOrderDetailBean>() {
            @Override
            public void onResponse(Call<CarOrderDetailBean> call, Response<CarOrderDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        detailProgressBarCarSettlement.setVisibility(View.GONE);
                        CarOrderDetailBean item = response.body();
                        DecimalFormat df = new DecimalFormat("0.00");
                        detailOrderCarTvMoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getRealPrice()) + "元");
                        detailOrderCarId.setText(item.getAttachment().getOrderFavourableModel().getOrderNum());
                        detailOrderCarNum.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getLicenseTag());
                        detailOrderCarBrand.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getBrand() + item.getAttachment().getOrderFavourableModel().getCarModel().getModel());
                        if (response.body().getAttachment().getOrderFavourableModel().getStatus() == 17 || response.body().getAttachment().getOrderFavourableModel().getStatus() == 103) {
                            detailOrderCarTvHaoshi.setText("15分钟");
                        } else {
                            detailOrderCarTvHaoshi.setText(TimeUtils.formatMinPlusOne((item.getAttachment().getOrderFavourableModel().getEndBillingTime() - item.getAttachment().getOrderFavourableModel().getStartBillingTime()) / 1000 + ""));
                        }
                        detailOrderCarOrderTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getOrderTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        detailOrderCarStartTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getStartBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        detailOrderCarEndTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getEndBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        detailOrderCarStartLocation.setText(item.getAttachment().getOrderFavourableModel().getOnLocationName());
                        detailOrderCarEndLocation.setText(item.getAttachment().getOrderFavourableModel().getDownLocationName());
                        //订单费用

                        detailOrderCarMoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getOrderPrice()) + "元");
                        //日间费用
                        String rj = df.format(item.getAttachment().getOrderFavourableModel().getDayMoney());
                        if (rj.equals("0.00")) {
                            detailOrderCarRjRl.setVisibility(View.GONE);
                            detailOrderCarRjLine.setVisibility(View.GONE);
                        } else {
                            detailOrderCarRjRl.setVisibility(View.VISIBLE);
                            detailOrderCarRjLine.setVisibility(View.VISIBLE);
                            detailOrderCarRjDj.setText("日间费用：" + df.format(item.getAttachment().getOrderFavourableModel().getCarModel().getValuationRuleModel().getWeekDayPrice() / 100d) + "元/分钟");
                            detailOrderCarRjmoney.setText(rj + "元");
                        }
                        //夜间费用
                        String wj = df.format(item.getAttachment().getOrderFavourableModel().getNightMoney());
                        if (wj.equals("0.00")) {
                            detailOrderCarWjRl.setVisibility(View.GONE);
                            detailOrderCarWjLine.setVisibility(View.GONE);
                        } else {
                            detailOrderCarWjRl.setVisibility(View.VISIBLE);
                            detailOrderCarWjLine.setVisibility(View.VISIBLE);
                            detailOrderCarWjDj.setText("晚间费用：" + df.format(item.getAttachment().getOrderFavourableModel().getCarModel().getValuationRuleModel().getWeekNightPrice() / 100d) + "元/分钟");
                            detailOrderCarWjmoney.setText(wj + "元");
                        }
                        //超时费用
                        String cs = df.format(item.getAttachment().getOrderFavourableModel().getTimeoutMoney());
                        if (cs.equals("0.00")) {
                            detailOrderCarCsRl.setVisibility(View.GONE);
                            detailOrderCarCsLine.setVisibility(View.GONE);
                        } else {
                            detailOrderCarCsRl.setVisibility(View.VISIBLE);
                            detailOrderCarCsLine.setVisibility(View.VISIBLE);
                            detailOrderCarCsDj.setText("超时费用：" + df.format(item.getAttachment().getOrderFavourableModel().getOverTimeMin() / 100d) + "元/分钟");
                            detailOrderCarCsmoney.setText(cs + "元");
                        }
                        //保险费用
                        if (item.getAttachment().getOrderFavourableModel().getOrderPrice() == 0) {
                            detailOrderCarBjmpRl.setVisibility(View.GONE);
                            detailOrderCarRsbzRl.setVisibility(View.GONE);
                        } else if (item.getAttachment().getOrderFavourableModel().getIsCompanyByRisk() == 1) {
                            detailOrderCarBjmpRl.setVisibility(View.VISIBLE);
                            detailOrderCarRsbzRl.setVisibility(View.VISIBLE);
                            detailOrderCarRsbzmoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getCompanyByRisk()) + "元");
                            detailorderCarBjmpmoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getAbatement()) + "元");
                        } else if (item.getAttachment().getOrderFavourableModel().getIsCompanyByRisk() == 0) {
                            detailOrderCarBjmpRl.setVisibility(View.VISIBLE);
                            detailOrderCarRsbzRl.setVisibility(View.GONE);
                            detailorderCarBjmpmoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getAbatement()) + "元");
                        }
                        //优惠费用
                        String yh = df.format(item.getAttachment().getOrderFavourableModel().getDaySpecialSub() +
                                item.getAttachment().getOrderFavourableModel().getBusinessWeekSpecialSub() +
                                item.getAttachment().getOrderFavourableModel().getWeekendSpecialSub() +
                                item.getAttachment().getOrderFavourableModel().getFullWeekSpecialSub());
                        if (yh.equals("0.00")) {
                            detailOrderCarRlYh.setVisibility(View.GONE);
                            detailOrderCarYhLine.setVisibility(View.GONE);
                            detailOrderCarYhBlock.setVisibility(View.GONE);
                        } else {
                            detailOrderCarRlYh.setVisibility(View.VISIBLE);
                            detailOrderCarYhLine.setVisibility(View.VISIBLE);
                            detailOrderCarYhBlock.setVisibility(View.VISIBLE);
                            detailOrderCarYhmoney.setText("-" + yh + "元");
                            detailOrderCarRzyhmoney.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getDaySpecialSub()) + "元");
                            detailOrderCarSwmoney.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getBusinessWeekSpecialSub()) + "元");
                            detailOrderCarZmthmoney.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getWeekendSpecialSub()) + "元");
                            detailOrderCarZzthmoney.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getFullWeekSpecialSub()) + "元");
                        }
                        //优惠券费用
                        detailOrderCarTvCoupon.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getCouponUserModel().getDenomination()) + "元");
                        if (item.getAttachment().getOrderFavourableModel().getPayType() == 1) {
                            detailOrderCarZffs.setText("支付宝");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 2) {
                            detailOrderCarZffs.setText("微信支付");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 3) {
                            detailOrderCarZffs.setText("个人账户支付");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 4) {
                            detailOrderCarZffs.setText("企业账户支付");
                        }
                        //碳排放
                        detailOrderCarTan.setText(item.getAttachment().getOrderFavourableModel().getCarbonEmission() + "");

                        //隐藏
                        if (item.getAttachment().getOrderFavourableModel().getStatus() == 102 || item.getAttachment().getOrderFavourableModel().getStatus() == 103) {
                            detailOrderCarIvHaoshi.setVisibility(View.GONE);
                            detailOrderCarRlHaoshi.setClickable(false);
                            flag = -1;
                        } else {
                            detailOrderCarIvHaoshi.setVisibility(View.VISIBLE);
                            detailOrderCarRlHaoshi.setClickable(true);
                            flag = -0;
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<CarOrderDetailBean> call, Throwable t) {
                detailProgressBarCarSettlement.setVisibility(View.GONE);
            }
        });
    }


    @OnClick({R.id.detail_order_car_iv_haoshi, R.id.detail_order_car_return, R.id.detail_order_car_phone, R.id.detail_order_car_rl, R.id.detail_order_car_rlYh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.detail_order_car_return:
                finish();
                break;
            case R.id.detail_order_car_phone:
                final KuaituDialog kuaituDialog2 = new KuaituDialog(mContext);
                kuaituDialog2.setTitle(getString(R.string.dialogTitle));
                kuaituDialog2.setMessage("是否拨打客服电话" + MyApplication.getInstance().getHotLine());
                kuaituDialog2.setYesOnclickListener("确定", new KuaituDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse("tel:" + MyApplication.getInstance().getHotLine()));
                        startActivity(intent);
                        kuaituDialog2.dismiss();
                    }
                });
                kuaituDialog2.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        kuaituDialog2.dismiss();
                    }
                });
                kuaituDialog2.show();
                break;
            case R.id.detail_order_car_rl:
            case R.id.detail_order_car_iv_haoshi:
                if (flag % 2 == 0) {
                    detailOrderCarRlHaoshi.setVisibility(View.VISIBLE);
                    detailOrderCarIvHaoshi.setImageResource(R.drawable.icon_details_up);
                    flag++;
                } else if (flag % 2 == 1) {
                    detailOrderCarRlHaoshi.setVisibility(View.GONE);
                    detailOrderCarIvHaoshi.setImageResource(R.drawable.icon_details_down);
                    flag++;
                }
                break;
            case R.id.detail_order_car_rlYh:
                if (flag1 % 2 == 0) {
                    detailOrderCarLlYh.setVisibility(View.VISIBLE);
                    detailOrderCarIvYhmoney.setImageResource(R.drawable.icon_details_up);
                    flag1++;
                } else if (flag1 % 2 == 1) {
                    detailOrderCarLlYh.setVisibility(View.GONE);
                    detailOrderCarIvYhmoney.setImageResource(R.drawable.icon_details_down);
                    flag1++;
                }
                break;
        }
    }

}
